package com.tanhua.dubbo.server.api;

import cn.hutool.core.collection.CollUtil;
import com.alibaba.dubbo.config.annotation.Service;
import com.tanhua.dubbo.server.pojo.*;
import lombok.extern.slf4j.Slf4j;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

import java.util.List;

@Service(version = "1.0.0")
@org.springframework.stereotype.Service
@Slf4j
public class SoulImpl implements SoulApi {
    @Autowired
    private MongoTemplate mongoTemplate;

    @Override
    public List<Soul> querySoul() {
        try {
            return mongoTemplate.findAll(Soul.class);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public List<SoulQuestion> querySoulQuestion(ObjectId id) {
        try {
            Query query = Query.query(Criteria.where("soulId").is(id.toString()));
            return mongoTemplate.find(query,SoulQuestion.class);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public List<Option> queryOption(String qid) {
        try {
            Query query = Query.query(Criteria.where("qid").is(qid));
            return mongoTemplate.find(query,Option.class);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public Boolean querySoulTestByUser(String userId, String sname) {
        try {
            Query query = Query.query(Criteria.where("userId").is(userId)
            .andOperator(Criteria.where("sname").is(sname)));
            List<SoulTime> soulTimes = mongoTemplate.find(query, SoulTime.class);
            if(CollUtil.isEmpty(soulTimes)){
                return false;
            }
            return true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    @Override
    public String result(Answer[] answers, String userId) {
        try {
            int sum = 0;
            for (Answer answer : answers) {
                Query query = Query.query(Criteria.where("id").is(answer.getOptionId()));
                Option option = mongoTemplate.findOne(query, Option.class);
                sum += option.getScore();
            }
            Query query = Query.query(Criteria.where("id").is(answers[0].getQuestionId()));
            SoulQuestion s = mongoTemplate.findOne(query, SoulQuestion.class);
            Query q = Query.query(Criteria.where("id").is(s.getSoulId()));
            Soul soul = mongoTemplate.findOne(q, Soul.class);
            SoulTime soulTime = new SoulTime();
            soulTime.setScore(sum);
            soulTime.setId(ObjectId.get());
            soulTime.setUserId(userId);
            soulTime.setSname(soul.getName());
            soulTime.setCreated(System.currentTimeMillis());
            mongoTemplate.save(soulTime);
            return soulTime.getId().toString();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public Integer queryScoreById(String id) {
        try {
            Query query = Query.query(Criteria.where("id").is(id));
            SoulTime soulTime = mongoTemplate.findOne(query, SoulTime.class);
            if(soulTime == null){
                return null;
            }
            return soulTime.getScore();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public Conclusion queryResultByScore(Integer score) {
        try {
            Query query = null;
            if(score < 21){
                query = Query.query(Criteria.where("id").is(1));
            }
            if(score >= 21 && score <= 40){
                query = Query.query(Criteria.where("id").is(2));
            }
            if(score >= 41 && score <= 55){
                query = Query.query(Criteria.where("id").is(3));
            }
            if(score >= 56){
                query = Query.query(Criteria.where("id").is(4));
            }
            Conclusion conclusion = mongoTemplate.findOne(query, Conclusion.class);
            if(conclusion != null){
                return conclusion;
            }
            return null;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public List<SoulTime> queryUserByUser(Integer score,String userId) {
        Integer min = score - 5;
        Integer max = score + 5;
        Query query = Query.query(Criteria.where("userId").nin(userId).andOperator(Criteria.where("score").gte(min),
                Criteria.where("score").lte(max)));
        query.limit(2);
        List<SoulTime> soulTimes = mongoTemplate.find(query, SoulTime.class);

        return soulTimes;
    }

}
